package com.jh3502.helper.function.batch.batchnew;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jh3502.helper.R;
import com.jh3502.helper.base.BaseActivity;
import com.jh3502.helper.utils.Constant;
import com.jh3502.helper.utils.SpHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author harisucici
 */
public class BatchNewActivity extends BaseActivity {
    private final static int LIST_RULE_CHOOSE = 999;
    @Nullable
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @Nullable
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @Nullable
    @BindView(R.id.batch_new_up_choose)
    RecyclerView batchNewUpChoose;
    @Nullable
    @BindView(R.id.batch_new_down_choose)
    RecyclerView batchNewDownChoose;
    private BatchNewAdapter batchNewUp, batchNewDown;
    @NonNull
    private List<BatchNewBean> batchUpList = new ArrayList<>();
    @NonNull
    private List<BatchNewBean> batchDownList = new ArrayList<>();
    @NonNull
    private List<Integer> batchUpListResult = new ArrayList<>();
    @NonNull
    private List<Integer> batchDownListResult = new ArrayList<>();

    @Override
    protected int setViewId() {
        return R.layout.activity_batch_new;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_save, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_save:

                SpHelper.getInstance().putString(Constant.CHOSEN_UPDERSS, "");
                SpHelper.getInstance().putString(Constant.CHOSEN_DOWNDERSS, "");

                int i = 0;
                int j = 0;
                for (BatchNewBean batchNewBean : batchUpList) {
                    if (batchNewBean.getIsCheck()) {
                        batchUpListResult.add(i);
                    }
                    i++;
                }
                for (BatchNewBean batchNewBean : batchDownList) {
                    if (batchNewBean.getIsCheck()) {
                        batchDownListResult.add(j);
                    }
                    j++;
                }
                SpHelper.getInstance().putString(Constant.CHOSEN_UPDERSS, new Gson().toJson(batchUpListResult));
                SpHelper.getInstance().putString(Constant.CHOSEN_DOWNDERSS, new Gson().toJson(batchDownListResult));
//                Validate.check(this, new SimpleValidateResult() {
//                    @Override
//                    public void onValidateSuccess() {
////                        ArrayList<Map<String, Object>> titleList = SpHelper.getInstance().getBatchList();
//                        personMap.put("batno", UUID.randomUUID());
//                        personMap.put("title", batchNewTitle.getText().toString().trim());
////                        titleList.add(map);
////                        SpHelper.getInstance().setBatchList(titleList);
//                        Intent intent = new Intent();
//                        intent.setClass(BatchNewActivity.this, ListRuleChooseActivity.class);
//                        startActivityForResult(intent, LIST_RULE_CHOOSE);
//                    }
//
//                    @Override
//                    public void onValidateError(String msg, EditText editText) {
//                        showShortToast(msg, SnackbarUtil.ALERT);
//                    }
//                });
                onBackPressed();
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("量体部位选择");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void initData() {
        String[] batchUpName = getResources().getStringArray(R.array.batch_normal_name);
        String[] batchDownName = getResources().getStringArray(R.array.batch_sp_name);
        for (String s : batchUpName) {
            BatchNewBean map = new BatchNewBean(false);
            map.setName(s);
            batchUpList.add(map);
        }
        for (String s : batchDownName) {
            BatchNewBean map = new BatchNewBean(false);
            map.setName(s);
            batchDownList.add(map);
        }
        StaggeredGridLayoutManager layoutManagerUp = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
        StaggeredGridLayoutManager layoutManagerDown = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);


        batchNewUp = new BatchNewAdapter(this, batchUpList);
        batchNewUpChoose.setLayoutManager(layoutManagerUp);
        batchNewUpChoose.setAdapter(batchNewUp);

        batchNewDown = new BatchNewAdapter(this, batchDownList);
        batchNewDownChoose.setLayoutManager(layoutManagerDown);
        batchNewDownChoose.setAdapter(batchNewDown);

        batchNewUp.setOnItemClickListener((view, pos, isCheck) -> {

            for (int i = 0; i < batchUpList.size(); i++) {
                batchUpList.get(pos).setIsCheck(true);
            }
            batchNewUp.notifyDataSetChanged();

        });
        batchNewDown.setOnItemClickListener((view, pos, isCheck) -> {

            for (int i = 0; i < batchDownList.size(); i++) {
                batchDownList.get(pos).setIsCheck(true);
            }
            batchNewDown.notifyDataSetChanged();

        });
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }

}